package com.mapfinal.server;

import com.jfinal.config.Interceptors;
import com.jfinal.config.Routes;
import com.jfinal.kit.StrKit;
import com.lambkit.Lambkit;
import com.lambkit.common.util.ScanJarStringSource;
import com.lambkit.core.rpc.RpcConfig;
import com.lambkit.db.datasource.ActiveRecordPluginWrapper;
import com.lambkit.module.LambkitModule;
import com.mapfinal.Mapfinal;
import com.mapfinal.platform.bufferedimage.BufferedImagePlatform;
import com.mapfinal.server.web.IndexController;
import com.mapfinal.server.web.WebInterceptor;

public class MapfinalServerModule extends LambkitModule {
	
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		super.configRoute(me);
		me.add("/", IndexController.class, "/mapfinal/html");
	}
	
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		super.configInterceptor(me);
		me.add(new WebInterceptor());
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Mapfinal.me().init(new BufferedImagePlatform(), new ServerMap());
	}

	@Override
	public void configMapping(ActiveRecordPluginWrapper arp) {
		if(StrKit.isBlank(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configMapping(String name, ActiveRecordPluginWrapper arp) {
		super.configMapping(name, arp);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbConfig())) {
			mapping(arp);
		}
	}
	
	public void mapping(ActiveRecordPluginWrapper arp) {
		arp.addSqlTemplate(new ScanJarStringSource("mapfinal.sql"));
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getRpcVersion();
	}
	public MapfinalServerConfig getConfig() {
		return Lambkit.config(MapfinalServerConfig.class);
	}
}
